package com.example.myapplication;

import android.content.Context;
import java.util.Calendar;
import java.util.List;

/**
 * 禁用时间段检查器
 * 检查当前时间是否在禁用时间段内
 */
public class BlockTimeChecker {

    private final BlockTimeRepository repository;
    private final Context context;

    public BlockTimeChecker(Context context) {
        this.context = context;
        this.repository = new BlockTimeRepository(context);
    }

    /**
     * 检查当前时间是否在禁用时间段内
     * @return true 表示被禁用，需要输入密码
     */
    public boolean isCurrentTimeBlocked() {
        List<BlockTimeSlot> blockTimes = repository.getAllBlockTimes();
        
        if (blockTimes.isEmpty()) {
            return false;
        }

        Calendar now = Calendar.getInstance();
        int currentHour = now.get(Calendar.HOUR_OF_DAY);
        int currentMinute = now.get(Calendar.MINUTE);
        int currentMinuteOfDay = currentHour * 60 + currentMinute;
        
        // 获取当前是星期几 (1=周日, 2=周一, ..., 7=周六)
        int dayOfWeek = now.get(Calendar.DAY_OF_WEEK);
        boolean isWeekend = (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY);

        for (BlockTimeSlot slot : blockTimes) {
            // 如果时间段未启用，跳过
            if (!slot.isEnabled()) {
                continue;
            }

            // 检查重复类型
            if (slot.getRepeatType() == BlockTimeSlot.RepeatType.NO_REPEAT) {
                // 不重复的时间段，暂时跳过（可以扩展为检查具体日期）
                continue;
            }

            if (slot.getRepeatType() == BlockTimeSlot.RepeatType.WORKDAYS) {
                // 法定工作日（周一到周五）
                if (isWeekend) {
                    continue; // 周末不生效
                }
                
                // 如果开启了"跳过节假日"，这里可以添加节假日判断逻辑
                if (slot.isSkipHolidays()) {
                    // TODO: 添加节假日API判断
                }
            }

            // 检查时间范围
            int startMinute = slot.getStartHour() * 60 + slot.getStartMinute();
            int endMinute = slot.getEndHour() * 60 + slot.getEndMinute();

            // 处理跨天的情况（如23:00 - 07:00）
            if (startMinute > endMinute) {
                // 跨天情况：23:00-07:00
                if (currentMinuteOfDay >= startMinute || currentMinuteOfDay < endMinute) {
                    return true; // 在禁用时间段内
                }
            } else {
                // 正常情况：08:00-16:30
                if (currentMinuteOfDay >= startMinute && currentMinuteOfDay < endMinute) {
                    return true; // 在禁用时间段内
                }
            }
        }

        return false;
    }

    /**
     * 获取当前生效的禁用时间段
     */
    public BlockTimeSlot getCurrentBlockedSlot() {
        List<BlockTimeSlot> blockTimes = repository.getAllBlockTimes();
        
        if (blockTimes.isEmpty()) {
            return null;
        }

        Calendar now = Calendar.getInstance();
        int currentHour = now.get(Calendar.HOUR_OF_DAY);
        int currentMinute = now.get(Calendar.MINUTE);
        int currentMinuteOfDay = currentHour * 60 + currentMinute;
        
        int dayOfWeek = now.get(Calendar.DAY_OF_WEEK);
        boolean isWeekend = (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY);

        for (BlockTimeSlot slot : blockTimes) {
            if (!slot.isEnabled()) {
                continue;
            }

            if (slot.getRepeatType() == BlockTimeSlot.RepeatType.NO_REPEAT) {
                continue;
            }

            if (slot.getRepeatType() == BlockTimeSlot.RepeatType.WORKDAYS && isWeekend) {
                continue;
            }

            int startMinute = slot.getStartHour() * 60 + slot.getStartMinute();
            int endMinute = slot.getEndHour() * 60 + slot.getEndMinute();

            if (startMinute > endMinute) {
                if (currentMinuteOfDay >= startMinute || currentMinuteOfDay < endMinute) {
                    return slot;
                }
            } else {
                if (currentMinuteOfDay >= startMinute && currentMinuteOfDay < endMinute) {
                    return slot;
                }
            }
        }

        return null;
    }

    /**
     * 获取当前禁用时间段的剩余时间（分钟）
     */
    public int getRemainingBlockMinutes() {
        BlockTimeSlot slot = getCurrentBlockedSlot();
        if (slot == null) {
            return 0;
        }

        Calendar now = Calendar.getInstance();
        int currentMinuteOfDay = now.get(Calendar.HOUR_OF_DAY) * 60 + now.get(Calendar.MINUTE);
        int endMinute = slot.getEndHour() * 60 + slot.getEndMinute();

        // 处理跨天情况
        if (slot.getStartHour() * 60 + slot.getStartMinute() > endMinute) {
            if (currentMinuteOfDay >= slot.getStartHour() * 60 + slot.getStartMinute()) {
                // 当前在今天的后半段，结束时间在明天
                return (24 * 60 - currentMinuteOfDay) + endMinute;
            } else {
                // 当前在今天的前半段，结束时间在今天
                return endMinute - currentMinuteOfDay;
            }
        } else {
            return endMinute - currentMinuteOfDay;
        }
    }
}

